package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipWarehouseStock;
import cc.comac.mom.bean.EquipWarehouseStockBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipWarehouseStockDao;
import cc.comac.mom.entity.EquipWarehouseStockEntity;
import cc.comac.mom.util.BaseEntityDataUtil;

@Component
@Transactional
public class EquipWarehouseStockDataHandler extends ServiceImpl<EquipWarehouseStockDao, EquipWarehouseStockEntity> {

	@Autowired
	private EquipWarehouseStockDao equipWarehouseStockDao;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	/**
	 * 单个新增库存
	 **/
	public boolean updateQuatityBySpareIdAndWarehouseId(EquipWarehouseStockEntity entity) {
		if (entity.getSparePartId() == 0) {
			throw new RuntimeException("备件编号不能为空");
		}
		if (StringUtils.isEmpty(entity.getWarehouseName())) {
			throw new RuntimeException("仓库名称");
		}
		if (entity.getQuantity() == 0) {
			throw new RuntimeException("入库数量不能为0");
		}
		// 校验库存中是否存在对应关系
		QueryWrapper<EquipWarehouseStockEntity> queryWrapper = new QueryWrapper<EquipWarehouseStockEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("spare_part_id", entity.getSparePartId());
		queryWrapper.eq("warehouse_name", entity.getWarehouseName());
		EquipWarehouseStockEntity selectOne = equipWarehouseStockDao.selectOne(queryWrapper);
		// 不存在新增
		if (selectOne == null) {
			baseEntityDataUtil.setBaseEntityData(entity);
			int insert = equipWarehouseStockDao.insert(entity);
			if (insert != 1) {
				throw new RuntimeException("新增库存失败！");
			}
		} else {
			// 存在更新
			selectOne.setQuantity(selectOne.getQuantity() + entity.getQuantity());
			baseEntityDataUtil.updateBaseEntityData(selectOne);
			int updateById = equipWarehouseStockDao.updateById(selectOne);
			if (updateById != 1) {
				throw new RuntimeException("更新库存失败！");
			}
		}
		return true;
	}

	/**
	 * 批量添加库存
	 **/
	public boolean updateQuatityBySpareIdAndWarehouseId(List<EquipWarehouseStockEntity> list) {
		List<EquipWarehouseStockEntity> saveList = new ArrayList<EquipWarehouseStockEntity>();
		List<EquipWarehouseStockEntity> updateList = new ArrayList<EquipWarehouseStockEntity>();
		for (EquipWarehouseStockEntity entity : list) {
			if (entity.getSparePartId() == 0) {
				throw new RuntimeException("备件编号不能为空");
			}
			if (StringUtils.isEmpty(entity.getWarehouseName())) {
				throw new RuntimeException("仓库名称");
			}
			if (entity.getQuantity() == 0) {
				throw new RuntimeException("入库数量不能为0");
			}
			// 校验库存中是否存在对应关系
			QueryWrapper<EquipWarehouseStockEntity> queryWrapper = new QueryWrapper<EquipWarehouseStockEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.eq("spare_part_id", entity.getSparePartId());
			queryWrapper.eq("warehouse_name", entity.getWarehouseName());
			queryWrapper.eq("location", entity.getLocation());
			EquipWarehouseStockEntity selectOne = equipWarehouseStockDao.selectOne(queryWrapper);
			// 不存在加入待新增集合
			if (selectOne == null) {
				baseEntityDataUtil.setBaseEntityData(entity);
				saveList.add(entity);
			} else {
				// 存在加入待更新集合
				selectOne.setQuantity(selectOne.getQuantity() + entity.getQuantity());
				baseEntityDataUtil.updateBaseEntityData(selectOne);
				updateList.add(selectOne);
			}
		}
			if (saveList.size() > 0) {
				boolean saveBatch = saveBatch(saveList, saveList.size());
				if (!saveBatch) {
					throw new RuntimeException("新增库存失败！");
				}
			}
			if (updateList.size() > 0) {
				boolean updateBatchById = updateBatchById(updateList, updateList.size());
				if (!updateBatchById) {
					throw new RuntimeException("更新库存失败");
				}
			}
		return true;
	}
	
	public boolean changeQuatityBySpareIdAndWarehouseId(List<EquipWarehouseStock> list) {
		List<EquipWarehouseStockEntity> updateList = new ArrayList<EquipWarehouseStockEntity>();
		for (EquipWarehouseStock entityBean : list) {
			EquipWarehouseStockEntity entity = entityBean.getInnerData();
			if (entity.getSparePartId() == 0) {
				throw new RuntimeException("备件编号不能为空");
			}
			if (StringUtils.isEmpty(entity.getWarehouseName())) {
				throw new RuntimeException("仓库名称");
			}
			if (entity.getQuantity() == 0) {
				throw new RuntimeException("入库数量不能为0");
			}
			if (StringUtils.isEmpty(entity.getLocation())) {
				throw new RuntimeException("本地位置吗不能为空！");
			}
			// 校验库存中是否存在对应关系
			QueryWrapper<EquipWarehouseStockEntity> queryWrapper = new QueryWrapper<EquipWarehouseStockEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.eq("spare_part_id", entity.getSparePartId());
			queryWrapper.eq("warehouse_name", entity.getWarehouseName());
			queryWrapper.eq("location", entity.getLocation());
			EquipWarehouseStockEntity selectOne = equipWarehouseStockDao.selectOne(queryWrapper);
				// 存在加入待更新集合
				selectOne.setQuantity(entity.getQuantity());
				baseEntityDataUtil.updateBaseEntityData(selectOne);
				selectOne.setLocation(entityBean.getCurrentLocation());
				updateList.add(selectOne);
			if (updateList.size() > 0) {
				boolean updateBatchById = updateBatchById(updateList, updateList.size());
				if (!updateBatchById) {
					throw new RuntimeException("更新库存失败");
				}
			}
		}
		return true;
	}
	
	public boolean validateLocationIsExist (String WarehouseName,long spareId,String location) {
		QueryWrapper<EquipWarehouseStockEntity> queryWrapper = new QueryWrapper<EquipWarehouseStockEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("spare_part_id", spareId);
		queryWrapper.eq("warehouse_name", WarehouseName);
		queryWrapper.eq("location", location);
		List<EquipWarehouseStockEntity> selectList = equipWarehouseStockDao.selectList(queryWrapper);
		if (selectList.size() > 0) {
			return false;
		}
		return true;
	}
	
	// 通过仓库查询仓库内所有备件主键信息
	public List<EquipWarehouseStockEntity> selectSpareIdsByWarehouseName (String warehouseName) {
		List<EquipWarehouseStockEntity> selectSpareIdsByWarehouseName = equipWarehouseStockDao.selectSpareIdsByWarehouseName(warehouseName);
		return selectSpareIdsByWarehouseName;
	}
	
	// 通过仓库和备件编码查询仓库内所有备件主键信息
		public EquipWarehouseStockEntity selectByWarehouseNameAndSpareNumber (String warehouseName,String sparePartNumber) {
			EquipWarehouseStockEntity selectByWarehouseNameAndSpareNumber = equipWarehouseStockDao.selectByWarehouseNameAndSpareNumber(sparePartNumber, warehouseName);
			return selectByWarehouseNameAndSpareNumber;
		}
		
		// 通过备件编号查询库存信息
		public ResultDev selectBySparePartId (EquipWarehouseStock equipWarehouseStock) {
			ResultDev resultDev = new ResultDev();
			EquipWarehouseStockEntity innerData = equipWarehouseStock.getInnerData();
			if (equipWarehouseStock.getPageNum() == 0) {
				equipWarehouseStock.setPageNum(1);
			}
			if (equipWarehouseStock.getPageSize() == 0) {
				equipWarehouseStock.setPageSize(10);
			}
			if (innerData == null) {
				throw new RuntimeException("无法检测到传入的数据！");
			}
			QueryWrapper<EquipWarehouseStockEntity> queryWrapper = new QueryWrapper<EquipWarehouseStockEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.eq("spare_part_id", innerData.getSparePartId());
			Page<EquipWarehouseStockEntity> page = new Page<EquipWarehouseStockEntity>(equipWarehouseStock.getPageNum(),equipWarehouseStock.getPageSize(),true);
			IPage<EquipWarehouseStockEntity> selectPage = equipWarehouseStockDao.selectPage(page, queryWrapper);
			resultDev.setEntityList(selectPage.getRecords());
			resultDev.setPageCount((int)page.getPages());
			resultDev.setTotalQuantity((int)page.getTotal());
			return resultDev;
		} 
	

		public ResultDev selectByFields(EquipWarehouseStockBean obj) {
			ResultDev resultDev = new ResultDev();
			if (obj.getPageSize() == 0) {
				obj.setPageSize(10);
			}
			obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
			List<EquipWarehouseStockBean> listEquipWarehouseStockBean = equipWarehouseStockDao.selectByFields(obj);
			long countByFields = equipWarehouseStockDao.selectCountByFields(obj);
			
			// 将查询总数除以每页数量后向上取整获取总页数
			double pageCount = Math.ceil((double)countByFields / obj.getPageSize());
			resultDev.setEntityList(listEquipWarehouseStockBean);
			resultDev.setPageCount((int)pageCount);
			resultDev.setTotalQuantity((int)countByFields);
			return resultDev;
		}
}
